package com.myspace.service.impl.account;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.myspace.mapper.UserMapper;
import com.myspace.pojo.User;
import com.myspace.service.space.account.GetUserListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.LinkedList;
import java.util.List;


@Service
public class GetUserListImpl implements GetUserListService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public JSONObject getUserList(Integer page) {
        IPage<User> userIPage = new Page<>(page, 5);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        List<User> users = userMapper.selectPage(userIPage, queryWrapper).getRecords();
        JSONObject resp = new JSONObject();
        List<JSONObject> items = new LinkedList<>();
        for(User user: users) {
            JSONObject item = new JSONObject();
            item.put("id", user.getId());
            item.put("username", user.getUsername());
            item.put("photo", user.getPhoto());
            item.put("follower_count", user.getFollowerCount());
            items.add(item);
        }

        resp.put("users", items);
        resp.put("users_count", userMapper.selectCount(null));

        return resp;
    }
}
